import jsonpath
import tkinter as tk
import os
import time
import score_update


class JScore:
      def __init__(self):
            if not(os.path.exists("json.json")):
                  score_update.main()
            
            a = eval(open('json.json','r',encoding='utf-8').read())
            self.res = jsonpath.jsonpath(a,'$..total_score')[1:]
            #print(type(self.res))


            print("总分=",self.res)
            b = a['data']['read_chapter']['rec_paper']['read_chapter']['sentence']
            self.words = []
            self.dp = []
            self.num = []

            for i in range (len(b)):#len(b)为句子的段数
                  j = b[i]['word']
                  self.num.append(len(j))
                  for k in range(len(j)):#len(j)为一句话的字数
            
                        self.words.append(j[k]['content'])
                        try:
                              self.dp.append(j[k]['syll']['dp_message'])
                        except Exception:
                              try:
                                    self.dp.append(j[k]['syll'][1]['phone'][1]['dp_message'])
                              except Exception:
                                    self.dp.append(j[k]['syll'][1]['dp_message'])
            #print(words)
            #print(dp)
            print("每句话的字数",self.num)
      
      def show(self,master:tk.Tk):
            self.root = master
            self.root.geometry('1800x1200')
            self.root.title('魔镜 V0.0.1 评分')
            self.page = tk.Frame(self.root)
            self.page.pack()
            text = tk.Text(self.page,width=50,height=20)
            text.pack()
            tk.Button(self.page,text='返回',command=self.back).pack(side='left',padx=10)

            text.tag_config("tag_0", foreground="green")
            text.tag_config("tag_16", foreground="red")
            text.tag_config("tag_32", foreground="gray")

            def insert(k):
                  dp_type = self.dp[k]
                  text.insert("insert",self.words[k],str('tag_'+dp_type))
                  
            # "insert" 索引表示插入光标当前的位置
            i = 0
            j = 0
            for k in range(len(self.words)):
                  if j==self.num[i]:
                        text.insert("insert",'\n')
                        insert(k)
                        i+=1
                        j=0
                  
                  else:
                        insert(k)
                        j+=1
            text.insert("insert",'\n')
            text.insert("insert",'\n')
            text.insert("insert",'总分='+self.res[1])
            text.insert("insert",'\n')
            text.insert("insert",'红色为有错，绿色为正确')
            self.root.mainloop()
            
           
      def back(self):
            os.remove('json.json')
            from Main import MainPage
            self.page.destroy()
            MainPage(master=self.root)
            


if __name__ == '__main__':
      JScore = JScore()
      JScore.show(root)
